Ext.define('WebOs.ex.DefaultController', {
	extend: 'Ext.app.Controller',
	
	onBtnResetEditForm : function(btn,evt,opts){
    	var editForm = btn.up('form');
    	editForm.getForm().reset();
    },
    
    onBtnCancelWindow : function(btn,evt,opts){
    	var win = btn.up('window');
    	if(win)
    		win.close();
    },
	
	onConfirmWindowBeforeClose : function(p,o){
	    var me=this;
    	var flag = p['dirty'];
    	if(!flag){
	    	var form = p.down('form');
	    	var flag = false;
	    	if(form){
	    		flag = form.getForm().isDirty();
	    	}
    	}
    	if(flag){
	    	Ext.MessageBox.show({
	    		title : '取消并关闭',
	    		msg : '执行取消操作后您的信息将不被保存，确认执行？',
	    		width : 200,
	    		buttons : Ext.MessageBox.OKCANCEL,
	    		//bodyCls : 'ux-window-alert-bgcolor',
	    		bodyStyle : {
	    			backgroundColor:'transparent'
	    		},
	    		icon : Ext.MessageBox.WARNING,
	    		fn : function(buttonId){
	    			var me=this;
			    	if(buttonId == 'ok'){
			    		p.destroy();
			    	}
	    		},
	    		scope : me
	    	});
    	}else{
    		p.destroy();
    	}
    	return false;
    },
	
	request : function(cfg){
		cfg = typeof cfg == 'object' ? cfg : {};
		if(cfg.masker){cfg.masker.el.mask("Loading...");}
		Ext.Ajax.request({
		    url: cfg.url||Constants.REQUEST_URL,
		    params :cfg.params||{e:''},
		    method : cfg.method||'POST',
		    success : function(response, opts){
		      	if(cfg.masker){cfg.masker.el.unmask();}
		      	var rs = response.responseText?Ext.decode(response.responseText):{};		
				if(rs.success){
					if(Ext.isFunction(cfg.onSuccess)){
						Ext.Function.bind(cfg.onSuccess,cfg.scope||window,[rs, opts])();
					}
				}else{
					if(Ext.isFunction(cfg.onFailure)){
						Ext.Function.bind(cfg.onFailure,cfg.scope||window,[rs, opts])();
					}else{
						Ext.Msg.show({
						     title:'错误提示',
						     msg: rs.msg||'Error!!',
						     width : 280,
						     buttons: Ext.Msg.OK,
						     icon: Ext.Msg.ERROR,
						     scope : cfg.scope,
						     closable : false
						});
					}
					if(rs.code == 3){
						location.reload();
					}
				}				
	      },
	      failure : function(response, opts){
		      	if(cfg.masker){cfg.masker.el.unmask();}
				var rs = response.responseText?Ext.decode(response.responseText):{};
				if(Ext.isFunction(cfg.onFailure)){
						Ext.Function.bind(cfg.onFailure,cfg.scope||window,[rs, opts])();
				}else{
					Ext.Msg.show({
					     title:'错误提示',
					     msg: rs.msg||'Error!!',
					     width : 280,
					     buttons: Ext.Msg.OK,
					     icon: Ext.Msg.ERROR,
					     scope : cfg.scope,
					     closable : false
					});
				}
				if(rs.code == 3){
					location.reload();
				}
	      }
		});
	}
});
